package com.gsk.europepharma.salesReport.struts.actions;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.Query;
import org.hibernate.Session;

import com.gsk.europepharma.salesReport.hibernate.dao.tbSpBoSRCartoContourDAO;
import com.gsk.europepharma.salesReport.hibernate.dao.tbSpBoSRSectoDAO;
import com.gsk.europepharma.salesReport.utils.UtilsSalesReport;

/**
 * Classe pour gerer les informations liees a un click sur la carto. 
 */
public class CartoEventClick extends Action {
	
	private final static int WIDTH = 500;
	private final static int HEIGHT = 500;
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		// Variables de session
		HttpSession sessionObject = request.getSession();
		Integer cs_adm_pdt = (Integer) sessionObject.getAttribute("cs_adm_pdt");
		Integer cs_adm_rgpt = (Integer) sessionObject.getAttribute("cs_adm_rgpt");
		Integer cs_adm_prstn = (Integer) sessionObject.getAttribute("cs_adm_prstn");
		Integer cs_network = (Integer) sessionObject.getAttribute("cs_network");
		Integer cs_month = (Integer) sessionObject.getAttribute("cs_month");
		// Fin de session
				
		tbSpBoSRCartoContourDAO cartoDAO = new tbSpBoSRCartoContourDAO();
		Session session = cartoDAO.getSession();
		Query req;
		String strReq = "";

		// Recuperation des parametres
		
		String source = request.getParameter("source");
		Integer CS_SRC = Integer.parseInt(source);
		String unite = request.getParameter("unite");
		String periode = request.getParameter("periode");
		
		String x = request.getParameter("x");
		Integer xInteger = Integer.parseInt(x);
		
		String y = request.getParameter("y");
		Integer yInteger = Integer.parseInt(y);
		
		Integer xA0 = -1;
		Integer yA0 = -1;
		Integer idUGA = -1;
		
		if (xInteger != -1 && yInteger != -1) {
			// Parametres de session 
			Integer xMinCarte1 = (Integer) sessionObject.getAttribute("xMinCarte2");
			Integer yMinCarte1 = (Integer) sessionObject.getAttribute("yMinCarte2");
			Integer maxCarte1 = (Integer) sessionObject.getAttribute("maxCarte2");
			
			xA0 = xMinCarte1 + xInteger * maxCarte1 / WIDTH;
			yA0 = yMinCarte1 + yInteger * maxCarte1 / HEIGHT;
			
			// Recuperation  de l'uga
			
			tbSpBoSRCartoContourDAO pixelDAO = new tbSpBoSRCartoContourDAO();
			session = pixelDAO.getSession();
			strReq = "";
			strReq = pixelDAO.QUERY_CLICK_UGA;
			req = session.getNamedQuery(strReq);
			req.setInteger("pos_x", xA0);
			req.setInteger("pos_y", yA0);
			req.setInteger("cs_network", cs_network);
			// On recupere le niveau (Secteur ou region) et l id de la carte de droite
			String nivGeogCartoS = (String) sessionObject.getAttribute("nivGeogCartoS");
			String idGeogCartoS = (String) sessionObject.getAttribute("idGeogCartoS");
			req.setInteger("idGeog", Integer.parseInt(idGeogCartoS)); 
			req.setString("nivGeog", nivGeogCartoS); 
			
			ArrayList result1 = new ArrayList(req.list());
			Iterator iter1 = result1.iterator();
			idUGA = -2;
			while (iter1.hasNext()) {
				idUGA =  Integer.parseInt(iter1.next().toString());
			}
		}

		// On a trouve l'id de l'UGA
		
		// Valeurs affichees dans la JSP
		String uga = "";
		String pm = "";
		String deltapm = "";
		String vol = "";
		String deltavol = "";
		String evol = "";
		
		if (idUGA > 0) { // On va chercher le libelle, la PM et le deltaPM de l UGA
			
			tbSpBoSRSectoDAO salesDAO = new tbSpBoSRSectoDAO();
			session = salesDAO.getSession();
			strReq = salesDAO.QUERY_RECUP_SALES;
			req = session.createSQLQuery(session.getNamedQuery(strReq).getQueryString());
			req.setString("data", unite + periode);	
			req.setInteger("cs_adm_pdt", cs_adm_pdt);
			req.setInteger("cs_adm_rgpt", cs_adm_rgpt);
			req.setInteger("cs_adm_prstn", cs_adm_prstn);
			req.setInteger("CS_SRC", CS_SRC);
			req.setInteger("cs_network", cs_network);
			req.setInteger("cs_month", cs_month);
			req.setString("nivGeog", "UGA"); 
			req.setInteger("idGeog", idUGA);
			req.setMaxResults(10);
			ArrayList result = new ArrayList(req.list());
			Iterator iter = result.iterator();
			while (iter.hasNext()) {
				Object[] val = (Object[]) iter.next();
				uga = val[0].toString();
				deltapm = UtilsSalesReport.getDeltaPm(Double.parseDouble(val[1].toString()), Double.parseDouble(val[2].toString()), Double.parseDouble(val[3].toString()), Double.parseDouble(val[4].toString()), 1) + "";
				pm = UtilsSalesReport.getPm(Double.parseDouble(val[1].toString()), Double.parseDouble(val[3].toString()), 1) + "";
				vol = UtilsSalesReport.getVol(Double.parseDouble(val[1].toString()), 0) + "";
				deltavol = UtilsSalesReport.getDeltaVol(Double.parseDouble(val[1].toString()), Double.parseDouble(val[2].toString()), 0) + "";
				evol = UtilsSalesReport.getEvolVol(Double.parseDouble(val[1].toString()), Double.parseDouble(val[2].toString()), 1) + "";
			}
			
		}
		
		// Envoie JSP
		HashMap<String, String> hm = new HashMap<String, String>();
		hm.put("uga", uga);
		hm.put("pm", pm);
		hm.put("deltapm", deltapm);
		hm.put("vol", vol);
		hm.put("deltavol", deltavol);
		hm.put("evol", evol);
		hm.put("idGeog", idUGA + "");

		JSONObject json = JSONObject.fromObject(hm);

		response.addHeader("Content-Type", "application/json");
        response.getOutputStream().write(json.toString().getBytes("UTF-8"));
        response.getOutputStream().flush();
	        
		return null;
	}

}
